package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.UserLocation;
import com.tanhua.domain.vo.UserLocationVo;
import com.tanhua.dubbo.api.mongo.UserLocationApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.Date;
import java.util.List;

@Service
public class UserLocationApiImpl implements UserLocationApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void saveLocation(Long userId, Double longitude, Double latitude, String addrStr) {
//        先判断这个用户是否有地理位置数据 如果有就是更新 如果没有就是新增

        Query query = new Query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        if(userLocation!=null){
//            save 和 insert的区别  save会根据id判断    insert直接插入

            Update update = new Update();
            update.set("location",new GeoJsonPoint(new Point(longitude,latitude)));
            update.set("address",addrStr);
            update.set("updated",new Date().getTime());
            update.set("lastUpdated",new Date().getTime());
            mongoTemplate.updateFirst(query,update,UserLocation.class);
        }else{
            userLocation = new UserLocation();
            userLocation.setUserId(userId);
            userLocation.setId(new ObjectId());
            //            MongoDB专门用来存储地理信息的对象GeoJsonPoint
            GeoJsonPoint geoJsonPoint = new GeoJsonPoint(longitude,latitude);
            userLocation.setLocation(geoJsonPoint); //经纬度
            userLocation.setAddress(addrStr);

            userLocation.setCreated(new Date().getTime());
            userLocation.setUpdated(new Date().getTime());
            userLocation.setLastUpdated(new Date().getTime());
            mongoTemplate.save(userLocation);
        }


    }

    @Override
    public List<UserLocationVo> search(Long userId, Integer distance) {
//        1、知道圆心
        Query query = new Query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        GeoJsonPoint location = userLocation.getLocation();
//        2、以当前登录人的位置为圆心画圆 圆的半径是distance

        Distance distance1 = new Distance( distance/1000 , Metrics.KILOMETERS);

//        3、画一个圆 Circle
        Circle circle = new Circle(location,distance1);

//        4、开始使用mongo查询

        Query circleQuery = new Query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocationList = mongoTemplate.find(circleQuery, UserLocation.class);

       return  UserLocationVo.formatToList(userLocationList);



    }
}
